package es.kix2902.testing.android.testing;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import es.kix2902.testing.android.R;

public class Widget extends AppWidgetProvider {

	static final ComponentName THIS_APPWIDGET = new ComponentName("es.kix2902.testing.android.testing", "es.kix2902.testing.android.testing.Widget");

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
	{
		final int N = appWidgetIds.length;

		for (int j=0; j<N; j++) {
			int appWidgetId = appWidgetIds[j];

			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);

			SharedPreferences prefs = context.getSharedPreferences("widget", 0);

			Boolean chk1 = prefs.getBoolean("chk1" + appWidgetId, false);
			Boolean chk2 = prefs.getBoolean("chk2" + appWidgetId, false);

			if (chk1==true)
			{
				views.setViewVisibility(R.id.btn_01, View.VISIBLE);

				Intent i = new Intent();
				i.setClass(context, WidgetSettings.class);
				i.addCategory(Intent.CATEGORY_ALTERNATIVE);
				i.setData(Uri.parse("custom:" + WidgetSettings.BUTTON_GOOGLE));
				PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

				views.setOnClickPendingIntent(R.id.btn_02, pi);
			}

			if (chk2==true)
			{
				views.setViewVisibility(R.id.btn_02, View.VISIBLE);

				Intent i = new Intent();
				i.setClass(context, WidgetSettings.class);
				i.addCategory(Intent.CATEGORY_ALTERNATIVE);
				i.setData(Uri.parse("custom:" + WidgetSettings.BUTTON_GMAIL));
				PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

				views.setOnClickPendingIntent(R.id.btn_02, pi);
			}

			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent)
	{
		if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE))
		{
			Uri data = intent.getData();
			int buttonId = Integer.parseInt(data.getSchemeSpecificPart());

			Log.d("gApps","Receiving intent" + buttonId);
			
			if (buttonId == WidgetSettings.BUTTON_GOOGLE)
			{
				Log.d("gApps","Intent to launch Google");
				Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com/m"));
				i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				context.startActivity(i);
			} else if (buttonId == WidgetSettings.BUTTON_GMAIL) {
				Log.d("gApps","Intent to launch gMail");
				Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://mail.google.com/m"));
				i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				context.startActivity(i);
			}
		}
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds)
	{
		final int N = appWidgetIds.length;

		for (int i=0; i<N; i++) {
			int appWidgetId = appWidgetIds[i];

			Editor editor = context.getSharedPreferences("widget", 0).edit();
			editor.remove("chk1" + appWidgetId);
			editor.remove("chk2" + appWidgetId);
			editor.commit();
		}
	}
}
